package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.RechargeMapper;
import com.translate.web.param.contract.IDateRangeParam;
import com.translate.web.param.contract.IIdAndUserIdParam;
import com.translate.entity.Recharge;
import com.translate.vo.contract.IOrderInfoVO;
import com.translate.web.service.IRechargeService;
import com.basic.service.impl.BaseServiceImpl;
import com.common.util.SnowFlake;
import com.param.contract.IPage;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RechargeServiceImpl extends BaseTranslateServiceImpl<Recharge> implements IRechargeService {
	public RechargeServiceImpl(RechargeMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(Recharge data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setPayStatus((byte) 0);
		data.setPayTime(0);
		data.setInvoicing((byte) 0);
	}

	protected Class<Recharge> getEntityClass() {
		return Recharge.class;
	}
	
	protected <S extends IPage> String sqlByGetCounttAfterProcesser(S param) {
		
		IDateRangeParam userIdParam = (IDateRangeParam) param;
		
		return this.sqlByGetCount("user_id = " + userIdParam.getUserId());
	}
	
	protected <S extends IPage> String sqlByGetPageListAfterProcesser(S param) {
		
		IDateRangeParam userIdParam = (IDateRangeParam) param;
		
		return this.sqlByGetPageList(" and user_id = " + userIdParam.getUserId() +" order by create_time desc");
	}
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, ? extends Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public <VO extends IOrderInfoVO> VO getOrderInfo(IIdAndUserIdParam userIdParam, Class<VO> type,  Byte isRegimental, int teamId) {
		String payOrderInfoKey = RedisConstant.getPayOrderInfoKey(userIdParam.getId(), userIdParam.getUserId());
		
		Recharge dataDetailFromCache = this.getDataDetailFromCache(userIdParam, payOrderInfoKey, 6);
		
		if (null == dataDetailFromCache) {
			return null;
		}
		
		if (1 != dataDetailFromCache.getPayStatus().intValue()) {
            throw new RuntimeException("请支付完成后，再申请发票");
        }
		
		try {
			VO orderInfo = type.newInstance();
			
			orderInfo.setId(dataDetailFromCache.getId());
			
			orderInfo.setOrderSnId(dataDetailFromCache.getOrderSn());
			
			orderInfo.setMoney(dataDetailFromCache.getAccount());
			
			return orderInfo;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int updateTraslate(int assocId) {

		RechargeMapper mapper = this.getBaseMapper();
		
		Recharge recharge = new Recharge();
		
		recharge.setId(assocId);
		
		recharge.setTranslate((byte) 1);
		
		
		return mapper.updateByPrimaryKeySelective(recharge);
	}
}